package com.t114514.demo.controller;

import com.t114514.demo.pojo.*;
import com.t114514.demo.service.QuestionnaireService;
import org.apache.ibatis.jdbc.Null;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import java.util.List;

@RestController
@RequestMapping("/questionnaire")
public class QuestionnaireController {
    @Autowired
    private QuestionnaireService questionnaireService;

    @GetMapping("/get")
    public Result<PageBean<Questionnaire>> getAllQuestionnaire(int pageNum, int pageSize) {
        PageBean<Questionnaire> questionnaireList= questionnaireService.getAllQuestionnaire(pageNum,pageSize);
        return Result.success(questionnaireList);
    }
//    @GetMapping("/getQuestionnaireList")
//    public Result<PageBean<Questionnaire>> getQuestionnaire(int teacherId,int pageNum, int pageSize) {
//        PageBean<Questionnaire> questionnaireList= questionnaireService.getQuestionnaire(teacherId,pageNum,pageSize);
//        return Result.success(questionnaireList);
//    }


    @PostMapping("/create-empty")
    public Result createEmptyQuestionnaire(String questionnaireTitle) {
        questionnaireService.create_empty(questionnaireTitle);
        return Result.success();
    }

    @PostMapping("/questions/add")
    public Result addQuestionToQuestionnaire(@RequestBody Question question) {
        questionnaireService.addQuestion(question);
        return Result.success();
    }

    @PostMapping("/questions/delete")
    public Result deleteQuestionToQuestionnaire(Integer questionId) {
        questionnaireService.deleteAllOption(questionId);
        questionnaireService.updateQuestionNumber(questionId);
        questionnaireService.deleteQuestion(questionId);
        return Result.success();
    }

    @PostMapping("/questions/options/add")
    public Result addOptionsToQuestion(@RequestBody Options option) {
        System.out.println(option);
        questionnaireService.addOptions(option);
        return Result.success();
    }

    @PostMapping("/questions/options/delete")
    public Result deleteOptionsToQuestionnaire(Integer optionId) {
        questionnaireService.updateOptionNumber(optionId);
        questionnaireService.deleteOption(optionId);
        return Result.success();
    }

    @GetMapping("/questions/get")
    public Result<PageBean<Question>> getQuestionOfQuestionnaire(Integer questionnaireId,int pageNum, int pageSize) {
        PageBean<Question> questionList =questionnaireService.getQuestions(questionnaireId,pageNum,pageSize);
        return Result.success(questionList);
    }

    @GetMapping("/questions/options/get")
    public Result getOptionsOfQuestion(Integer questionId) {
        List<Options> optionList=  questionnaireService.getOptions(questionId);
        return Result.success(optionList);
    }

    @PostMapping("/submit")
    public Result submitQuestionnaire(@RequestBody Questionnaire questionnaire) {
        List<Integer>  questionIdList   =questionnaireService.checkQuestionnaire(questionnaire);
        if(questionIdList.isEmpty()){

            List<TeacherAndStudent> teacherAndStudents =   questionnaireService.getAllTeacherAndStudent();
            System.out.println(teacherAndStudents);
            for(TeacherAndStudent teacherAndStudent:teacherAndStudents){
                int studentId=teacherAndStudent.getStudentId();
                int teacherId=teacherAndStudent.getTeacherId();
                int questionnaireId=questionnaire.getQuestionnaireId();
                questionnaireService.publishQuestionnaire(questionnaireId,studentId,teacherId);
            }

            questionnaireService.submitQuestionnaire(questionnaire);
            return Result.success();
        }
        return Result.error("单选题至少有两个选项");
    }



}
